package io.renren.modules.crud.controller;

import java.util.Arrays;
import java.util.Map;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.mail.MailUtil;
import com.tencent.common.pojo.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.crud.entity.InformationEntity;
import io.renren.modules.crud.service.InformationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 信息表
 *
 * @author syh
 * @email sunlightcs@gmail.com
 * @date 2022-05-19 09:40:52
 */
@RestController
@RequestMapping("crud/information")
public class InformationController {
    @Autowired
    private InformationService informationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("crud:information:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = informationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{infoId}")
    @RequiresPermissions("crud:information:info")
    public R info(@PathVariable("infoId") Integer infoId){
		InformationEntity information = informationService.getById(infoId);

        return R.ok().put("information", information);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("crud:information:save")
    public R save(@RequestBody InformationEntity information){
		informationService.save(information);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("crud:information:update")
    public R update(@RequestBody InformationEntity information){
		informationService.updateById(information);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("crud:information:delete")
    public R delete(@RequestBody Integer[] infoIds){
		informationService.removeByIds(Arrays.asList(infoIds));

        return R.ok();
    }

    /**
     * 判断个人信息是否通过或已存在
     * @param productId
     * @return
     */
//    @RequestMapping("getByProId")
//    public Result<InformationEntity> getByProId(@RequestParam("productId") Integer productId){
//        return informationService.productId(productId);
//    }

    /**
     * 审核更改状态
     * @param infoId
     * @param infoStatus
     */
    @RequestMapping("examination")
    public void examination(@RequestParam("infoId") Integer infoId,@RequestParam("infoStatus") Integer infoStatus){
        informationService.examination(infoId,infoStatus);
    }

    public static void main(String[] args) {
//        MailUtil.send("2826324267@qq.com", "广大金融", "<h1>邮件来自广大金融</h1>", true, FileUtil.file("C:\\Users\\LENOVO\\Desktop\\贷款合同.pdf"));

//        double invest = 200000;     //贷款本金
//        double yearRate = 0.042;   //年利率
//        double monthRate = yearRate/12;   //月利率
//
////      int year = 15;     //还款年数
//        int month = 20*12;  //还款月数
//
//        System.out.println("本金-->"+invest+"   年利率--->"+yearRate*100+"%"+"  期限--->"+month+"个月");
//        System.out.println("--------------------------------------------");
//
//        // 每月本息金额  = (本金×月利率×(1＋月利率)＾还款月数)÷ ((1＋月利率)＾还款月数-1))
//        double monthIncome = (invest* monthRate * Math.pow(1+monthRate,month))/(Math.pow(1+monthRate,month)-1);
//        System.out.println("每月本息金额 : " + monthIncome);
//        System.out.println("---------------------------------------------------");
//
//        // 每月本金 = 本金×月利率×(1+月利率)^(还款月序号-1)÷((1+月利率)^还款月数-1))
//        double monthCapital = 0;
//        for(int i=1;i<month+1;i++){
//            monthCapital = (invest* monthRate * (Math.pow((1+monthRate),i-1)))/(Math.pow(1+monthRate,month)-1);
//            System.out.println("第" + i + "月本金： " + monthCapital);
//        }
//        System.out.println("---------------------------------------------------");
//
//
//        // 每月利息  = 剩余本金 x 贷款月利率
//        double monthInterest = 0;
//        double capital = invest;
//        double tmpCapital = 0;
//        double totalInterest = 0;
//        for(int i=1;i<month+1;i++){
//            capital = capital - tmpCapital;
//            monthInterest = capital * monthRate;
//            tmpCapital = (invest* monthRate * (Math.pow((1+monthRate),i-1)))/(Math.pow(1+monthRate,month)-1);
//            System.out.println("第" + i + "月利息： " + monthInterest);
//            totalInterest = totalInterest + monthInterest;
//        }
//
//        System.out.println("-------------------------------------------------");
//        System.out.println("利息：--->"+totalInterest);
//
//        System.out.println("-------------------------------------------------");
//        System.out.println("年利率：--->"+totalInterest/month*12/invest*100+"%");
//    }

    }
}
